
import Foundation

public extension Date {
    
    public func year() -> Int {
        return (Calendar.current as NSCalendar).component(.year, from: self)
    }
    
    public func month() -> Int {
        return (Calendar.current as NSCalendar).component(.month, from: self)
    }
    
    public func day() -> Int {
        return (Calendar.current as NSCalendar).component(.day, from: self)
    }
    
    public func hour() -> Int {
        return (Calendar.current as NSCalendar).component(.hour, from: self)
    }
    
    public func minute() -> Int {
        return (Calendar.current as NSCalendar).component(.minute, from: self)
    }
    
    public func second() -> Int {
        return (Calendar.current as NSCalendar).component(.second, from: self)
    }
    
    /// 1: Sunday ... 7: Saturday
    public func weekday() -> Int {
        return (Calendar.current as NSCalendar).component(.weekday, from: self)
    }
    
    /// Format date to string: yyyy年MM月dd日
    public func stringDate() -> String {
        
        var strDate = self.description
        
        var dateFormatter: DateFormatter?
        
        if dateFormatter == nil {
            
            dateFormatter = DateFormatter()
            dateFormatter?.dateFormat = "yyyy年MM月dd日"
            dateFormatter?.locale = Locale.current
            
        }
        
        strDate = dateFormatter!.string(from: self)
        
        return strDate
    }
    
    /// Format date to string: HH:mm
    public func stringMinute() -> String {
        
        var stringMinute = self.description
        
        var dateFormatter: DateFormatter?
        
        if dateFormatter == nil {
            
            dateFormatter = DateFormatter()
            dateFormatter?.dateFormat = "HH:mm"
            dateFormatter?.locale = Locale.current
            
        }
        
        stringMinute = dateFormatter!.string(from: self)
        
        return stringMinute
    }
    
    /// Format date to string: yyyy年MM月dd日 HH:mm
    public func stringMinuteDate() -> String {
        
        var strDate = self.description
        
        var dateFormatter: DateFormatter?
        
        if dateFormatter == nil {
            
            dateFormatter = DateFormatter()
            dateFormatter?.dateFormat = "yyyy年MM月dd日 HH:mm"
            dateFormatter?.locale = Locale.current
            
        }
        
        strDate = dateFormatter!.string(from: self)
        
        return strDate
    }
    
    /// Format date to string: yyyy年MM月dd日 HH:mm:ss
    public func stringSecondDate() -> String {
        
        var strDate = self.description
        
        var dateFormatter: DateFormatter?
        
        if dateFormatter == nil {
            
            dateFormatter = DateFormatter()
            dateFormatter?.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
            dateFormatter?.locale = Locale.current
        }
        
        strDate = dateFormatter!.string(from: self)
        
        return strDate
    }
    
    /// Format date to string as format
    public func stringDateByStringFormat(format: String) -> String {
        
        var strDate = self.description
        
        var dateFormatter: DateFormatter?
        
        if dateFormatter == nil {
            
            dateFormatter = DateFormatter()
            dateFormatter?.dateFormat = format
            dateFormatter?.locale = NSLocale.current
        }
        
        strDate = dateFormatter!.string(from: self)
        
        return strDate
    }
    
    
    /// calculate the days from now to the target date
    ///
    /// - Parameter date: target date
    /// - Returns: days between now to the target date
    public static func dayDifference(from date: Date) -> Int {
        let calendar = NSCalendar.current
        let startOfNow = calendar.startOfDay(for: date)
        let startOfTimeStamp = calendar.startOfDay(for: Date())
        let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
        let day = components.day!
        if day < 1 {
            return 0
        } else {
            return day
        }
    }

    
    /// ISO8601格式时间转为Date时间
    ///
    /// - Parameter string: ISO8601格式时间
    /// - Returns: Date时间
    public static func date(fromISO8601 string: String?) -> Date? {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

        if let dateString = string {
            return dateFormatter.date(from: dateString)
        }
        return nil
    }

    /// Date时间转为ISO8601格式时间
    ///
    /// - Returns: ISO8601格式时间
    public func stringFromISO8601() -> String {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
        
        let dateString = dateFormatter.string(from: self)
        return dateString
    }
}

